import { Component, OnInit } from '@angular/core';
import { Classroom } from '../../model/classroom';
import { CourseService } from '../../service/course.service';
import { Router } from '@angular/router';

@Component({
  selector: 'app-classroom',
  templateUrl: './classroom.component.html',
  styleUrls: ['./classroom.component.css']
})
export class ClassroomComponent implements OnInit {
  m:Classroom;
  ml:Array<Classroom>
  constructor(
    private courseservice:CourseService,
    private router:Router) {
      this.m=new Classroom();
     }

  ngOnInit() {
    this.loadClassroomList();
  }

  addClassroom(){
    this.m=new Classroom();
  }

  save(){
    if(this.m.id){
      this.update();
    }
    else{
      this.add();
    }
  }

  goBack(){
    this.router.navigate(['courseindex']);
  }

  add(){
    this.courseservice.addClassroom(this.m)
    .then((flag:boolean)=>{
    if(flag== true){
      this.m =new Classroom();
      this.loadClassroomList()
    }
    else{
      alert("添加异常，请重试");
    }
    })
  }

  update(){
    this.courseservice.updateClassroom(this.m)
    .then((flag:boolean)=>{
     if(flag == true){
       this.m= new Classroom();
       this.loadClassroomList();
     }
     else{
       alert("修改异常，请重试");
     }
    })
  }

  toupdate(m:Classroom){
    this.m=m;
  }

  deleteClassroom(m:Classroom){
    this.courseservice.deleteClassroom(m)
    .then((flag:boolean)=>{
     if(flag == true){
       this.loadClassroomList();
     }
     else{
       alert("删除异常，请重试");
     }
    })
  }

  loadClassroomList(){
    this.ml= new Array();
    this.courseservice.getClassroomList().then((data:any)=>{
      this.ml = data;
    })

  }





}
